/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-28     Administrator       the first version
 */
#ifndef APPLICATIONS_DEVICE_MOTION_MOTION_H_

#define APPLICATIONS_DEVICE_MOTION_MOTION_H_


#include <rtdef.h>
#include "tmc5160.h"
#include "GlobalVar.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct
{
    rt_uint32_t TOFF;
    rt_uint32_t HSTRT;
    rt_uint32_t HEND;
    rt_uint32_t TBL;
    rt_uint32_t CHM;
    rt_uint32_t I_HOLD;
    rt_uint32_t I_RUN;
    rt_uint32_t TPOWERDOWN;
    rt_uint32_t EN_PWM_MODE;
    rt_uint32_t TPWM_THRS;
    rt_uint32_t POSITIVE_DIR;
}TMC5160_INIT_PARA;

typedef struct
{
    uint32_t vstart;
    uint32_t a1;
    uint32_t v1;
    uint32_t amax;
    uint32_t vmax;
    uint32_t dmax;
    uint32_t d1;
    uint32_t vstop;
    uint32_t rampmode;
    int32_t  x_target;
    int32_t  x_actual;
}TMC5160_RUN_PARA;

typedef struct
{
    uint32_t vstart;
    uint32_t a1;
    uint32_t v1;
    uint32_t amax;
    uint32_t vmax;
    uint32_t dmax;
    uint32_t d1;
    uint32_t vstop;
}TMC5160_RES_RUN_PARA;

typedef enum {
    GENERAL,
    ADVANCED
}GoHome_Mode;

typedef enum {
    POSITION,
    VELOCITY_CW,
    VELOCITY_ACW
}RUN_MODE;

class TmcDriver
{
public:
    TmcDriver(rt_uint8_t _driver_num);
    virtual ~TmcDriver();
private:
    rt_int8_t driver_num;
    TMC5160_INIT_PARA init_param;
    TMC5160_RUN_PARA run_param;
    TMC5160_RES_RUN_PARA res_run_param[10];
public:
    rt_uint32_t init(void);
    rt_uint32_t getRunMode(void);
    rt_uint32_t startup(void);
    rt_uint32_t moveToPositon(rt_int32_t position);
    rt_uint32_t moveByVelocity(float32_t f_velocity);
    rt_uint32_t moveToPosByVelocity(rt_int32_t position, float32_t velocity);
    rt_uint32_t goHome(GoHome_Mode mode);
    rt_uint32_t goHomeByVelocity(float32_t velocity);
    rt_uint8_t positionReached(void);
    rt_uint8_t velocityReached(void);
    rt_uint8_t stopReached(void);
    rt_uint8_t positionCheck(void);
    rt_uint8_t getCurPosition(void);
    rt_uint8_t goHomeMinimAccCal(void);
    rt_uint8_t SetTargetPos(rt_int32_t position);
public:
    void gohome_task(void *parameter);
    void run_task(void *parameter);
    void liquidDet_task(void *parameter);
};

extern TmcDriver *tmc_driver[7];
extern void (*Motor_D1_Task_Ptr[7]) (void *parameter) ;

void Motor_D0_Task(void *parameter);
void Motor_D1_Task(void *parameter);
void Motor_D2_Task(void *parameter);
void Motor_D3_Task(void *parameter);
void Motor_D4_Task(void *parameter);
void Motor_D5_Task(void *parameter);
void Motor_D6_Task(void *parameter);

#ifdef __cplusplus
}
#endif


#endif /* APPLICATIONS_DEVICE_MOTION_MOTION_H_ */
